/**
 * @file TaskMgr.cpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 任务管理器
 * @version 0.0.1
 * @date 2020-07-18
 * 
 * @coryright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */

#include "TaskMgr.hpp"
#include "STL_ThreadPool.hpp"
#include <functional>
#include "DBMgr.hpp"
#include "STL_Log.hpp"
#include <eXosip2/eX_setup.h>
#include <string.h>
#include "GBMgr.hpp"
#include "Global.hpp"
#include "SDPHandler.hpp"

TaskMgr *TaskMgr::instance()
{
    static TaskMgr _ins;
    return &_ins;
}

TaskMgr::TaskMgr(/* args */)
{
}

TaskMgr::~TaskMgr()
{
}

void TaskMgr::start()
{
    stl::ThreadTask recvTask = std::bind(&TaskMgr::DoScanTasks, this);
    stl::ThreadPool::instance()->pushTask(recvTask);
}

int TaskMgr::DoScanTasks()
{
    static int dump = 0;
    if (dump++ < 10)
    {
        return 0;
    }
    dump = 0;

    // stl::debug("DoScanTasks");

    std::map<int, DBMgr::TaskStatus> tasks = DBMgr::instance()->getTaskList();
    std::map<int, DBMgr::TaskStatus>::iterator it;
    for (it = tasks.begin(); it != tasks.end(); it++)
    {
        if (it->second == DBMgr::TaskStatus::NEW)
        {
            int deviceId = it->first;
            stl::debug("Start Task[%d]", deviceId);
            DBMgr::instance()->setTask(deviceId, DBMgr::TaskStatus::WORK);
            auto deviceInfo = DBMgr::instance()->getDevice(deviceId);
            send_invite_sdp(g_excontext, deviceInfo);
        }
    }
}

/*
INVITE sip:41010200001310000001@10.0.0.47:5060 SIP/2.0
Via: SIP/2.0/UDP 172.20.0.1:15060;rport;branch=z9hG4bK831558840
From: <sip:34020000002000000001@3402000000>;tag=937558840
To: <sip:41010200001310000001@10.0.0.47:5060>
Call-ID: 997558736
CSeq: 59 INVITE
Content-Type: APPLICATION/SDP
Contact: <sip:34020000002000000001@172.20.0.1:15060>
Max-Forwards: 70
User-Agent: LiveGBS v200714
Subject: 41010200001310000001:0102000001,34020000002020000001:0
Content-Length: 214

v=0
o=34020000002000000001 0 0 IN IP4 10.0.0.42
s=Play
c=IN IP4 10.0.0.42
t=0 0
m=video 30016 RTP/AVP 96 97 98
a=recvonly
a=rtpmap:96 PS/90000
a=rtpmap:97 MPEG4/90000
a=rtpmap:98 H264/90000
y=0102000001
*/

int TaskMgr::send_invite_sdp(eXosip_t *context_eXosip, DBMgr::Device deviceInfo)
{
    std::string channel_code  = "41010200001310000001";// TODO 通过查询获取<CmdType>Catalog</CmdType>

    int ret = 0;
    osip_message_t *invitesip = NULL;

    // std::string from = g_sip_from_ipport;
    std::string from = g_sip_from_domain;
    std::string to = "sip:" + channel_code + "@" + deviceInfo.host + ":" + std::to_string(deviceInfo.port);

    eXosip_lock(context_eXosip);
    ret = eXosip_call_build_initial_invite(context_eXosip, &invitesip, to.c_str(), from.c_str(), NULL, "");
    if (ret < 0)
    {
        stl::error("(%t)%D [GB28181ServerTask] send_invite_sdp eXosip_call_build_initial_invite fail,from:%s,to:%s", from.c_str(), to.c_str());
        return -1;
    }
    ret = eXosip_call_send_initial_invite(context_eXosip, invitesip);
    if (ret < 0)
    {
        stl::error("(%t)%D [GB28181ServerTask] send_invite eXosip_call_send_initial_invite fail,from:%s,to:%s", from.c_str(), to.c_str());
        return -1;
    }

    {
        std::string sdpcontent = SDPHandler::generateSDP(g_local_sip_cfg_svr_id, g_local_sip_cfg_svr_ip, 13398, "0102000001");
        osip_message_set_body(invitesip, sdpcontent.c_str(), sdpcontent.length());
        osip_message_set_content_type(invitesip, "application/sdp");
    }

    ret = eXosip_call_send_initial_invite(context_eXosip, invitesip);
    eXosip_unlock(context_eXosip);
    if (ret < 0)
    {
        stl::error("(%t)%D [GB28181ServerTask] send_invite_sdp eXosip_call_send_initial_invite fail,from:%s,to:%s", from.c_str(), to.c_str());
        return -1;
    }

    stl::info("(%t)%D [GB28181ServerTask] send_invite_sdp from:%s,to:%s\n", from.c_str(), to.c_str());
    return ret;
}
